package com.example.test003.controller;

import com.alibaba.fastjson.JSON;
import com.example.test003.domain.entity.Myweathers;
import com.example.test003.mapper.MyWeatherMapper;
import com.example.test003.pojo.LocationAndClientId;
import com.example.test003.pojo.WeatherStation;
import com.example.test003.service.LocationAndClientIdService;
import com.example.test003.service.MyWeatherService;
import com.example.test003.service.WeatherStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
public class WeatherStationController {
    @Autowired
    private WeatherStationService weatherStationService;

    @Autowired
    private LocationAndClientIdService locationAndClientIdService;
    @Autowired
    private MyWeatherMapper myWeatherMapper;


    //将数字气象站数据传给前端
    @GetMapping(value = "/queryWeatherstation")
    public String getWeatherstation(String time,String location){
        if ("南京信息工程大学站".equals(location)){
            List<WeatherStation> weathers=weatherStationService.queryWeatherStation(time);
            String Json_weather= JSON.toJSONString(weathers);
            System.out.println(Json_weather);
            return Json_weather;
        }else if ("superadmin".equals(location)){
            List<WeatherStation> weathers=weatherStationService.queryWeatherStation(time);
            String Json_weather= JSON.toJSONString(weathers);
            System.out.println(Json_weather);
            return Json_weather;
        } else if ("大方站".equals(location)) {
            List<WeatherStation> weathers=weatherStationService.queryWeatherStationDaFang(time);
            String Json_weather= JSON.toJSONString(weathers);
            System.out.println(Json_weather);
            return Json_weather;
        } else {
            LocationAndClientId locationAndClientId=locationAndClientIdService.queryByLocation(location);//根据location地点查询设备码
            String clientid=locationAndClientId.getClientId();
            List<Myweathers> myweathers = myWeatherMapper.queryWeatherStationByClientId(time,clientid);
            String Json_weather= JSON.toJSONString(myweathers);
            System.out.println(Json_weather);
            return Json_weather;
        }

    }

    //请求数字气象站数据
    @PostMapping(value = "/getWeatherstation")
    public void  getWeatherstation1(@RequestBody Myweathers myweathers){
        System.out.printf("降水：");
        System.out.println(myweathers.getRainfall());

        Double press=Double.valueOf(myweathers.getPressure())*10;
        String xxx=String.format("%.2f",press);
        myweathers.setPressure(String.valueOf(Double.parseDouble(xxx)));
        if (Objects.isNull(myweathers.getWindDirection())){
            myweathers.setWindDirection("无数据");
        }
        else if (myweathers.getWindDirection().equals("0")){
            myweathers.setWindDirection("南风");
        }else if(myweathers.getWindDirection().equals("90")){
            myweathers.setWindDirection("西风");
        } else if (myweathers.getWindDirection().equals("180")){
            myweathers.setWindDirection("北风");
        } else if (myweathers.getWindDirection().equals("270")){
            myweathers.setWindDirection("东风");
        }else if (myweathers.getWindDirection().equals("45")){
            myweathers.setWindDirection("西南风");
        } else if (myweathers.getWindDirection().equals("135")){
            myweathers.setWindDirection("西北风");
        } else if (myweathers.getWindDirection().equals("225")){
            myweathers.setWindDirection("东北风");
        } else if (myweathers.getWindDirection().equals("315")){
            myweathers.setWindDirection("东南风");
        }

        String time=myweathers.getDetectedTime();
        LocalDateTime dateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String deviceTime = dateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
        myweathers.setDetectedTime(deviceTime);
        Date now = new Date();

        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMdd");
        String formattedDateTime2 = formatter2.format(now);

        String time0 = formattedDateTime2+"0000";
        String time0_1 = formattedDateTime2+"0001";
        String time1 = formattedDateTime2+"0100";
        String time1_1 = formattedDateTime2+"0101";
        String time2 = formattedDateTime2+"0200";
        String time2_1 = formattedDateTime2+"0201";
        String time3 = formattedDateTime2+"0300";
        String time3_1 = formattedDateTime2+"0301";
        String time4 = formattedDateTime2+"0400";
        String time4_1 = formattedDateTime2+"0401";
        String time5 = formattedDateTime2+"0500";
        String time5_1 = formattedDateTime2+"0501";
        String time6 = formattedDateTime2+"0600";
        String time6_1 = formattedDateTime2+"0601";
        String time7 = formattedDateTime2+"0700";
        String time7_1 = formattedDateTime2+"0701";
        String time8 = formattedDateTime2+"0800";
        String time8_1 = formattedDateTime2+"0801";
        String time9 = formattedDateTime2+"0900";
        String time9_1 = formattedDateTime2+"0901";
        String time10 = formattedDateTime2+"1000";
        String time10_1 = formattedDateTime2+"1001";
        String time11 = formattedDateTime2+"1100";
        String time11_1 = formattedDateTime2+"1101";
        String time12 = formattedDateTime2+"1200";
        String time12_1 = formattedDateTime2+"1201";
        String time13 = formattedDateTime2+"1300";
        String time13_1 = formattedDateTime2+"1301";
        String time14 = formattedDateTime2+"1400";
        String time14_1 = formattedDateTime2+"1401";
        String time15 = formattedDateTime2+"1500";
        String time15_1 = formattedDateTime2+"1501";
        String time16 = formattedDateTime2+"1600";
        String time16_1 = formattedDateTime2+"1601";
        String time17 = formattedDateTime2+"1700";
        String time17_1 = formattedDateTime2+"1701";
        String time18 = formattedDateTime2+"1800";
        String time18_1 = formattedDateTime2+"1801";
        String time19 = formattedDateTime2+"1900";
        String time19_1 = formattedDateTime2+"1901";
        String time20 = formattedDateTime2+"2000";
        String time20_1 = formattedDateTime2+"2001";
        String time21 = formattedDateTime2+"2100";
        String time21_1 = formattedDateTime2+"2101";
        String time22 = formattedDateTime2+"2200";
        String time22_1 = formattedDateTime2+"2201";
        String time23 = formattedDateTime2+"2300";
        String time23_1 = formattedDateTime2+"2301";

        myweathers.setWindSpeed((myweathers.getWindSpeed()+"m/s"));
        //把温度湿度改成整数
        Double tem =Double.valueOf(myweathers.getAmbientTemperature());
        double tem2 = Math.floor(tem * 10) / 10;
//        int tem_int = (int)tem2;
        myweathers.setAmbientTemperature((tem2+"°C"));

        Double hum =Double.valueOf(myweathers.getAmbientHumidity());
        double hum2 =Math.floor(hum* 10) / 10;
//        int hum_int = (int)hum2;

        myweathers.setAmbientHumidity((hum2+"%"));
        myweathers.setPressure((myweathers.getPressure()+"hpa"));
        myweathers.setRainfall((myweathers.getRainfall()+"mm"));

//        myWeatherMapper.insertWeatherStation(myweathers);

        Myweathers myweathersFromSql=null;
        if(time0.equals(deviceTime)||time0_1.equals(deviceTime)){
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time0);
            if (myweathersFromSql!=null){
                //System.out.println("0点数据");
            }else {
                myweathers.setDetectedTime(time0);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("0点数据 ");
                System.out.println(myweathers);
            }
        } else if (time1.equals(deviceTime)||time1_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time1);
            if (myweathersFromSql!=null){
                //System.out.println("1点数据");
            }else {
                myweathers.setDetectedTime(time1);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("1点数据 ");
                System.out.println(myweathers);
            }

        }else if (time2.equals(deviceTime)||time2_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time2);
            if (myweathersFromSql!=null){
                //System.out.println("2点数据");
            }else {
                myweathers.setDetectedTime(time2);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("2点数据 ");
                System.out.println(myweathers);
            }

        }else if (time3.equals(deviceTime)||time3_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time3);
            if (myweathersFromSql!=null){
                //System.out.println("3点数据");
            }else {
                myweathers.setDetectedTime(time3);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("3点数据 ");
                System.out.println(myweathers);
            }

        }else if (time4.equals(deviceTime)||time4_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time4);
            if (myweathersFromSql!=null){
                //System.out.println("4点数据");
            }else {
                myweathers.setDetectedTime(time4);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("4点数据 ");
                System.out.println(myweathers);
            }

        }else if (time5.equals(deviceTime)||time5_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time5);
            if (myweathersFromSql!=null){
                //System.out.println("5点数据");
            }else {
                myweathers.setDetectedTime(time5);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("5点数据 ");
                System.out.println(myweathers);
            }

        }else if (time6.equals(deviceTime)||time6_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time6);
            if (myweathersFromSql!=null){
                //System.out.println("6点数据");
            }else {
                myweathers.setDetectedTime(time6);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("6点数据 ");
                System.out.println(myweathers);
            }

        }else if (time7.equals(deviceTime)||time7_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time7);
            if (myweathersFromSql!=null){
                //System.out.println("7点数据");
            }else {
                myweathers.setDetectedTime(time7);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("7点数据 ");
                System.out.println(myweathers);
            }

        }else if (time8.equals(deviceTime)||time8_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time8);
            if (myweathersFromSql!=null){
                //System.out.println("8点数据");
            }else {
                myweathers.setDetectedTime(time8);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("8点数据 ");
                System.out.println(myweathers);
            }

        }else if (time9.equals(deviceTime)||time9_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time9);
            if (myweathersFromSql!=null){
                //System.out.println("9点数据");
            }else {
                myweathers.setDetectedTime(time9);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("9点数据 ");
                System.out.println(myweathers);
            }

        }else if (time10.equals(deviceTime)||time10_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time10);
            if (myweathersFromSql!=null){
                //System.out.println("10点数据");
            }else {
                myweathers.setDetectedTime(time10);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("10点数据 ");
                System.out.println(myweathers);
            }

        }else if (time11.equals(deviceTime)||time11_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time11);
            if (myweathersFromSql!=null){
                //System.out.println("11点数据");
            }else {
                myweathers.setDetectedTime(time11);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("11点数据 ");
                System.out.println(myweathers);
            }

        }else if (time12.equals(deviceTime)||time12_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time12);
            if (myweathersFromSql!=null){
                //System.out.println("12点数据");
            }else {
                myweathers.setDetectedTime(time12);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("12点数据 ");
                System.out.println(myweathers);
            }

        }else if (time13.equals(deviceTime)||time13_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time13);
            if (myweathersFromSql!=null){
                //System.out.println("13点数据");
            }else {
                myweathers.setDetectedTime(time13);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("13点数据 ");
                System.out.println(myweathers);
            }

        }else if (time14.equals(deviceTime)||time14_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time14);
            if (myweathersFromSql!=null){
                //System.out.println("14点数据");
            }else {
                myweathers.setDetectedTime(time14);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("14点数据 ");
                System.out.println(myweathers);
            }

        }else if (time15.equals(deviceTime)||time15_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time15);
            if (myweathersFromSql!=null){
                //System.out.println("15点数据");
            }else {
                myweathers.setDetectedTime(time15);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("15点数据 ");
                System.out.println(myweathers);
            }

        }else if (time16.equals(deviceTime)||time16_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time16);
            if (myweathersFromSql!=null){
                //System.out.println("16点数据");
            }else {
                myweathers.setDetectedTime(time16);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("16点数据 ");
                System.out.println(myweathers);
            }

        }else if (time17.equals(deviceTime)||time17_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time17);
            if (myweathersFromSql!=null){
                //System.out.println("17点数据");
            }else {
                myweathers.setDetectedTime(time17);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("17点数据 ");
                System.out.println(myweathers);
            }

        }else if (time18.equals(deviceTime)||time18_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time18);
            if (myweathersFromSql!=null){
                //System.out.println("18点数据");
            }else {
                myweathers.setDetectedTime(time18);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("18点数据 ");
                System.out.println(myweathers);
            }

        }else if (time19.equals(deviceTime)||time19_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time19);
            if (myweathersFromSql!=null){
                //System.out.println("19点数据");
            }else {
                myweathers.setDetectedTime(time19);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("19点数据 ");
                System.out.println(myweathers);
            }

        }else if (time20.equals(deviceTime)||time20_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time20);
            if (myweathersFromSql!=null){
                //System.out.println("20点数据");
            }else {
                myweathers.setDetectedTime(time20);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("20点数据 ");
                System.out.println(myweathers);
            }

        }else if (time21.equals(deviceTime)||time21_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time21);
            if (myweathersFromSql!=null){
                //System.out.println("21点数据");
            }else {
                myweathers.setDetectedTime(time21);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("21点数据 ");
                System.out.println(myweathers);
            }

        }else if (time22.equals(deviceTime)||time22_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time22);
            if (myweathersFromSql!=null){
                //System.out.println("22点数据");
            }else {
                myweathers.setDetectedTime(time22);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("22点数据 ");
                System.out.println(myweathers);
            }

        }else if (time23.equals(deviceTime)||time23_1.equals(deviceTime)) {
            myweathersFromSql=myWeatherMapper.QueryWeatherStation(time23);
            if (myweathersFromSql!=null){
                //System.out.println("23点数据");
            }else {
                myweathers.setDetectedTime(time23);
                myWeatherMapper.insertWeatherStation(myweathers);
                System.out.print("23点数据 ");
                System.out.println(myweathers);
            }

        }else {
            //System.out.println("不是整点数据");
        }
//        System.out.println(myweathers);
    }
}
